// JavaScript Document


function popUp(w, h, t, l,  url, name)
{
	var name = (name) ? name : 'popup';
	var win = window.open(url, name, 'resizable, width=' + w +', height=' + h + ', top=' + t + ', left=' + l);
	win.focus();
}





/* Getting Browser Info
------------------------------------------------------------------------------------------- */
var isNS = 0;
var isIE = 0;
var isOtherBrowser;
	
if(navigator.appName.indexOf('Netscape') != -1 ) isNS = 1;
else {
	if(navigator.appName.indexOf('Microsoft Internet Explorer') != -1) isIE = 1;
	else isOtherBrowser = 1;	
}



/* Simple Redirect */
function Redirect(url)
{
	if(url) window.location = url;
	else window.location = "./";
}



/* AJAX Functions
-------------------------------------------------------------------------------------------- */

// Setting
var AjaxPath = 'http://localhost/sms/ajax/';


// XML HTTP GET 
function xmlhttpGet(strURL) 
{
    var xmlHttpReq;
	
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
    xmlHttpReq.open('GET', strURL, false);
	
	/*
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
            // alert (xmlHttpReq.responseText);
        }
	}
	*/
	
	xmlHttpReq.send(null);
	return xmlHttpReq.responseText;
}


// XML HTTP GET Complex 
function makeAsynxRequest(url, func) 
{
	if(window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		request = new ActiveXObject("MSXML2.XMLHTTP");
	}
	
	request.onreadystatechange = eval(func);
	request.open("GET", url, true); // last param make request asynx
	request.send(null);
}


function makeSynxRequest(url) 
{
	var request;

	if(window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		request = new ActiveXObject("MSXML2.XMLHTTP");
	}
	
	request.open("GET", url, false); // last param make request synx
	request.send(null);
	return request.responseText;
}





/* Standard Input Validation Functions and String Functions
--------------------------------------------------------------------------------------------- */

// Trimming String
function trim(string) 
{
  string = string.toString();
  while (string.substring(0,1) == ' ') {
    string = string.substring(1,string.length);
  }
  while (string.substring(string.length-1,string.length) == ' ') {
    string = string.substring(0,string.length-1);
  }
  return string;
}

function clearPhoneNumber(string)
{
	return string.replace(/[\+\-\(\)\{\}\s]/g, '');
}


function clearNonDigits(string)
{
	return string.replace(/[^\d]/g, '');
}

// Checking if String empty
function isEmpty(arg)
{
	arg = arg.toString();
	if(trim(arg.length) == 0) 
		return true;
	else 
		return false;
}

// Checking if arg is object
function isObject(obj)
{
	if(typeof obj == 'object') 
		return true;
	else
		return false;
}
	

// Checking if argument is valid string for username
function isValidUsername(string)
{
	if( string.search(/^[A-Za-z0-9]{3,40}$/) == -1 )
	{
		return false;	
	}
	else
	{
		return true;
	}
}

// Checking if string can be validated as Email Address
function isValidEmail(string) 
{
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
			return true;
	else	return false;
}


// Checks if String is valid as Phone number
function isValidInternationalPhone(string)
{
	string = clearPhoneNumber(string);
	if( string.search(/^[0-9]{8,13}$/) == -1 )
	{
		return false;	
	}
	else
	{
		if(string.substr(0, 1) == '0') return false;
		else return true;
	}
}


// Checks if String is valid as Phone number
function isValidUSPhone(string)
{
	// string = clearPhoneNumber(string);
	if( string.search(/^[0-9]{10,13}$/) == -1 )
	{
		return false;	
	}
	else
	{
		if(string.substr(0, 1) == '0') return false;
		else return true;
	}
}


// Checking if string's lengh is more than 5 and does not contain spaces, tabs etc ...
function isValidPassword(string)
{
	if(string.search(/\s/) >= 0) return false;
	else if(string.length < 5) return false;
	else return true;
}
